%macro prnt (program=, disname=, idisname=, ds_list=, popfl=);
*** Info from adsl and assigning overall column***;

%get_data(lst=adae,pre=adam,supp =N,lib=adam);

proc format;
 value treat
  1 = "Study Product A"
  2 = "Study Product B"
  3 = "Study Product C"
  ;
quit;

data adsl;
length trtc $200. trt 8.;
 set adam.adsl;
 where SAFFL="Y";
arma=strip(substr(actarmcd,1,3));
if index(actarmcd,"/") then armb=strip(substr(actarmcd,5)); 
if index(arma, 'A') then do;
trt = 1;
    trtc = "Study Product A";
    output;
    end;
if index(arma, 'B') then do;
trt = 2;
    trtc = "Study Product B";
    output;
    end;
if index(arma, 'C') then do;
trt = 3;
    trtc = "Study Product C";
    output;
    end;
run;

data adsl_over;
length trtc $200. trt 8.;
set adam.adsl; 
  if SAFFL="Y" ;
    trt = 9;
    trtc = "Part A Overall";
   output;
run;

data adsl;
set adsl adsl_over;
run;

proc sort data=adsl; by usubjid trt; run;

*** Info from adae and assigning overall column***;
data adae;
length trtc $200. trt 8.;
set adam.adae;
if SAFFL="Y" and trtan in (1 2 3);
if aebodsys eq " " then aebodsys = "Unassigned";
if aedecod eq " " then aedecod = "Unassigned";
*In related category include AE noted related and possible and in not related, the remaining AE, i.e. Unlikely/remote and not related.;
/*if arel in ("Possibly Related" "Probably Related") then rel=1; 
	else if arel in ("Not Related") then rel=2;*/
if areln = 1 then rel = 1; 	
else if areln = 2 then rel = 2;	
else if areln = 3 then rel = 3;	
else if areln = 4 then rel = 4;

     if aebodsys ne " " then do;
     if trta='A' then do;
      trt = 1;
    trtc = "Study Product A";
end;
if trta='B' then do;
      trt = 2;
    trtc = "Study Product B";
end;
if trta='C' then do;
      trt = 3;
    trtc = "Study Product C";
end;
end;
  output;
      if SAFFL="Y";
    trt = 9;
    trtc = "Part A Overall";
   output;
run;

data adae1;
length trtc $200. trt 8.;
set adam.adae;
if SAFFL="Y" and trtan in (1 2 3);
if aebodsys eq " " then aebodsys = "Unassigned";
if aedecod eq " " then aedecod = "Unassigned";
*In related category include AE noted related and possible and in not related, the remaining AE, i.e. Unlikely/remote and not related.;
/*if arel in ("Possibly Related" "Probably Related") then rel=1; 
	else if arel in ("Not Related") then rel=2;*/
if areln = 1 then rel = 1; 	
else if areln = 2 then rel = 2;	
else if areln = 3 then rel = 3;	
else if areln = 4 then rel = 4;

if aebodsys ne " " then do;
     if trta='A' then do;
      trt = 1;
    trtc = "Study Product A";
end;
if trta='B' then do;
      trt = 2;
    trtc = "Study Product B";
end;
if trta='C' then do;
      trt = 3;
    trtc = "Study Product C";
end;
end;
run;

data adae2;
length trtc $200. trt 8.;
set adam.adae;
if SAFFL="Y" and TRTAN in (1 2 3);
if aebodsys eq " " then aebodsys = "Unassigned";
if aedecod eq " " then aedecod = "Unassigned";
*In related category include AE noted related and possible and in not related, the remaining AE, i.e. Unlikely/remote and not related.;
/*if arel in ("Possibly Related" "Probably Related") then rel=1; 
	else if arel in ("Not Related") then rel=2;*/
if areln = 1 then rel = 1; 	
else if areln = 2 then rel = 2;	
else if areln = 3 then rel = 3;	
else if areln = 4 then rel = 4;
if aebodsys ne " " then do;
     if trta='A' then do;
      trt = 1;
    trtc = "Study Product A";
end;
if trta='B' then do;
      trt = 2;
    trtc = "Study Product B";
end;
if trta='C' then do;
      trt = 3;
    trtc = "Study Product C";
end;
end;
run;



***POP counts; 
proc sql noprint;
 create table tpop as 
  select count(distinct usubjid) as count, trt from adsl where saffl='Y' group by trt;
 create table teae_pop as 
  select count(usubjid) as count, trt from adae where trtemfl="Y"
  group by trt;
quit;

data _null_;
 set tpop;  
  if count ne . then call symputx("N"||strip(put(trt,1.)), strip(put(count,3.)), "G");
  else if count eq . then call symputx("N"||strip(put(trt,1.)), "0", "G");
run;
%put &n1 &n2 &n3 &n9;

%macro freq_count(num=, where1=, where2=);
Proc sql noprint; 
create table freq&num as 
 select trt,trtc,count(usubjid) as count_pt ,&num as ord 
 from adae1 &where1.
 group by trt,trtc
 outer union corr
 select 9 as trt,"Part A Overall" as trtc, count(usubjid) as count_pt ,&num as ord /*overall column */
 from adae1 &where2.;
quit;
%mend;

%macro freq_count_(num=, where1=, var=);
Proc sql noprint; 
create table freq&num as 
 select trt,trtc,count(usubjid) as count_pt ,&num as ord &var. 
 from adae &where1. 
 group by trt,trtc &var.;
quit;
%mend;

%freq_count(num=1, where1=%str(), where2=%str(where AOCC01FL="Y")); /* Adverse Events Reported [n] */
%freq_count(num=2, where1=%str(where aoccfl="Y"), where2=%str(where aocc19fl="Y")); /* Subjects With At Least One TEAE [n(%)] [1] */

%freq_count(num=3, where1=%str(where AOCC01FL="Y"), where2=%str(where aocc15fl="Y")); /* Treatment Emergent Adverse Events (TEAEs) Reported [n] */
%freq_count(num=4, where1=%str(where aocctfl="Y"), where2=%str(where aocc20fl="Y")); /* Subjects With At Least One TEAE [n(%)] [1] */

%freq_count_(num=5, where1=%str(where trtemfl="Y"), var=%str(,rel)); /* TEAEs Relationship [2] */
%freq_count_(num=6, where1=%str(where trtemfl="Y" and asevn^=.), var=%str(,asevn)); /* TEAEs Severity/Intensity [2] */

%freq_count(num=7, where1=%str(where aocc05fl="Y"), where2=%str(where aocc18fl="Y")); /* Serious Adverse Events (SAEs) Reported[n][2]  */
%freq_count(num=8, where1=%str(where aocc10fl="Y"), where2=%str(where aocc22fl="Y")); /* Subjects With At Least One SAE [n(%)][1] */
%freq_count(num=9, where1=%str(where aocc12fl="Y"), where2=%str(where aocc24fl="Y")); /* TEAE leading to withdrawal [n(%)][1]  */

%freq_count_(num=10, where1=%str(where aesdth="Y"), var=); /* Death [n(%)] [1]  */

data freq;
 set freq1-freq10; run;

proc sort data=freq; by trt ord rel asevn; run;

/* Create dummy dataset as per SAP */
data dummy;
length txt $100.;
do trt = 1, 2, 3, 5, 9;
ord=1; rel=.; asevn=.; txt="Adverse Events Reported [n]"; output; 
ord=2; rel=.; asevn=.; txt="Subjects With At Least One AE [n(%)] [1]"; output; 
ord=3; rel=.; asevn=.; txt="Study Product Use-Emergent Adverse Events (SPUEAEs) Reported [n]"; output; 
ord=4; rel=.; asevn=.; txt="Subjects With At Least One SPUEAE [n(%)] [1]"; output;
ord=5; rel=0; asevn=.; txt="SPUEAEs Relationship [2]"; output;
ord=5; rel=1; asevn=.; txt="      Unrelated [n(%)]"; output; 
ord=5; rel=2; asevn=.; txt="      Unlikely Related [n(%)]"; output;
ord=5; rel=3; asevn=.; txt="      Possibly Related [n(%)]"; output;
ord=5; rel=4; asevn=.; txt="      Probably Related [n(%)]"; output;

ord=6; rel=.; asevn=0; txt="SPUEAEs Severity/Intensity [2]"; output;
ord=6; rel=.; asevn=1; txt="      Mild [n(%)]"; output; 
ord=6; rel=.; asevn=2; txt="      Moderate [n(%)]"; output;
ord=6; rel=.; asevn=3; txt="      Severe [n(%)]"; output; 
ord=7; rel=.; asevn=.; txt="Study Product Use-Emergent Serious Adverse Events (SPUESAEs) Reported [n] [2]"; output; 
ord=8; rel=.; asevn=.; txt="Subjects With At Least One SPUESAE [n(%)][1]"; output; 
ord=9; rel=.; asevn=.; txt="SPUEAEs Leading To Study Discontinuation Reported [n] [2]"; output; 
ord=10; rel=.; asevn=.; txt="SPUEAEs Leading To Death Reported [n] [2]"; output;
end;
run;

/* Merge with dummy */
data freqs;
merge dummy freq;
by trt ord rel asevn;
if ord in (1,2) then delete; 
run;

/* calculating percentages and converting numeric counts and percents to character */
data freq_;
length treat $20;
 merge freqs tpop teae_pop(rename=(count=count_te));
 by trt; 
 if ord in (2 4 8 ) then do;
     if count_pt ^in (. 0) then pc=(count_pt /count)*100;
     if count_pt ^in (. 0) then treat=put(count_pt,3.)||" ("||trim(left(put(pc,5.1)))||')';
     else if count_pt in (. 0) then treat= "  0";
	end;
 else if ord in (5 6 7 9 10) and rel ne 0 and asevn ne 0 then do;
     if count_pt ^in (. 0) then pc=(count_pt /count_te)*100;
/*      if count_pt ^in (. 0) then pc=(count_pt /count)*100;*/

     if count_pt ^in (. 0) then treat=put(count_pt,3.)||" ("||trim(left(put(pc,5.1)))||')';
     else if count_pt in (. 0) then treat= "  0";
	end;
 else do;
   if count_pt ne . then treat=put(count_pt,3.);
    else if rel ne 0 and asevn ne 0 then treat= "  0";end;
run;

proc sort data=freq_; by ord rel asevn trt; where trt ne .; run;

proc transpose data=freq_ out=freq_t prefix=col;
by ord rel asevn txt;
var treat;
id trt;
run;

data report;
 set freq_t;
 page=1;
 if txt='' then delete; 
run;

/* For header */
data adsl;
  set adsl(drop=arm);
 trtp=trtc; column=trt; arm=trtc;
run;
  
Proc sort data =adsl out =tofmt nodupkey ;
   by column arm;
run;

proc sort data=adsl out=pop nodupkey;
 by usubjid trt;
run;
%popu(datain=POP);
%makefmt2(tofmt, column ,trtp, ftrt);
%heads(fmt=ftrt);

%let nt=“Probably Related” or “Possibly Related”.;

*******************************************************************************************************
Printing using proc report
******************************************************************************************************;
title3 " ";
title4 "Table &disname (Continued)";
%footer1(foot1=1, ds_list=&ds_list., program=&program.);
footnote3 j=l "[1] Percentages are based on the number of subjects in the Safety Population in each Study Product group.";
footnote4 j=l "[2] Percentages are based on the total number of Study Product Use-Emergent adverse events reported in each Study Product group.";

/*footnote5 j=l "[3] AE that was reported as either: &nt"*/

filename filetmp temp;
filename filertf "\\algopharm.com\algorithmepharmadata\Biostudies\Montreal\&prot.\SRA\Biostatistics\Work\Outputs\tables\T&idisname..SUM.AE.rtf";

%open_rtf;
%empty_ds_fix(report);
title5 "Summary of Adverse Events – Part A";
title6 "(Safety Population)";

proc report data=report nowd split='~' missing
style(header)=[background=white  borderbottomcolor=white bordertopcolor=black  background=_undef_ ] ;
/* column page ord rel asevn txt ("\brdrb\brdrs\brdrw11 Dose THC"  col1 col2 col3) col9;*/
  column page ord rel asevn txt col1 col2 col3 col9;

 define page         / " " order order=data noprint;
 define ord       / " " order order=data noprint;
 define rel          / " " order order=data noprint;
 define asevn          / " " order order=data noprint;

 define txt /display "Parameter"  "&linebot." order order=data style(column)=[cellwidth=4in just=left] style(header)=[just=left];
 /*define col1 /display "System Organ Class~  " order=internal style(column)=[cellwidth=2.5in just=left] style(header)=[just=left];  */  
 define col1 / "&linebot. &header1. " style(column)=[cellwidth=1.4in just=C] style(header)=[just=C];
 define col2 / "&linebot. &header2. " style(column)=[cellwidth=1.4in just=C] style(header)=[just=C];
 define col3 / "&linebot. &header3. " style(column)=[cellwidth=1.4in just=C] style(header)=[just=C];
 define col9 / "&linebot. &header9. " style(column)=[cellwidth=1.4in just=C] style(header)=[just=C];
/* define col9 / "&linebot. Overall~ " style(column)=[cellwidth=1.3in just=C] style(header)=[just=C];*/

 break after page / page;

 compute after ord / style=[cellwidth=6in just=left];
  line " ";
 endcomp;

 run;

%close_rtf;
%arrange_rtf;

%mend prnt;

%prnt(program=T14_03_01_01_01_SUM_AE, disname=14.3.1.1.1, idisname=14.3.1.1.1, ds_list=ADAE ADSL, popfl=%str(SAFFL ='Y'));
%clrw;
